package juc.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fangbin
 * @desc 演示原子类的基本用法
 * @date 2020/6/13 10:39
 */
public class AtomicIntegerDemo  implements Runnable{

    private static final AtomicInteger atomicInteger = new AtomicInteger();
    public void incrementAtomic(){
        atomicInteger.getAndIncrement();
    }
    private static  volatile  int basicCount = 0;

    public void incrementBasic(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        basicCount++;
    }

    @Override
    public void run() {
        for(int i=0;i<1000;i++){
            incrementAtomic();
            incrementBasic();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AtomicIntegerDemo atomicIntegerDemo = new AtomicIntegerDemo();
        Thread t1 = new Thread(atomicIntegerDemo);
        Thread t2  = new Thread(atomicIntegerDemo);
        Thread t3  = new Thread(atomicIntegerDemo);
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println(atomicInteger);
        System.out.println(basicCount);
    }
}
